//
//  goosCell.m
//  ios-supplier
//
//  Created by Benlai on 15/7/31.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "GoodsCell.h"
#import "MemoryData.h"

#define MERGIN 10
#define LIST_WIDTH_PERCENT  0.21
#define GOODS_HEIGHT 90
#define TITLE_HEIGHT 27
#define WEIGHT_HEIGHT 26
#define TITLE_FONT_SIZE 14
#define WEIGHT_FONT_SIZE 14
#define SYMBOL_WIDTH 10
#define ADDBTN_WIDTH 27


@implementation GoodsCell 
{
    UIImageView *goodsImage;
    UILabel *titleLabel;
    UILabel * _highQuality;
    UILabel *weightLabel;
    UILabel *symbolLabel;
    UILabel *priceLabel;
    UIButton *addBtn;
//    UILabel *buyNumberLabel;
    UIButton *buyNumberBtn;
    UIButton *subBtn;
    CGFloat goodsWidth;
    NSInteger buyNumber;
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellWidth:(CGFloat)cellWidth
{
    goodsWidth = cellWidth;
    return [self initWithStyle:style reuseIdentifier: reuseIdentifier];
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle: style reuseIdentifier: reuseIdentifier];
    if (self) {
        buyNumber = 0;
        goodsImage = [[UIImageView alloc]initWithFrame: CGRectMake(MERGIN, MERGIN, GOODS_HEIGHT - MERGIN * 2, GOODS_HEIGHT - MERGIN * 2)];
        goodsImage.layer.borderWidth = 1;
        goodsImage.layer.borderColor = [UIColor colorFromHexValue: 0xe8e8e8].CGColor;
        [self.contentView addSubview: goodsImage];
        
        _highQuality = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(goodsImage.frame) + MERGIN, MERGIN +3, 30 + 8, TITLE_HEIGHT - 7)];
        
        _highQuality.layer.borderWidth = 1;
        _highQuality.text = @"精品";
        _highQuality.layer.borderColor = [[UIColor redColor] CGColor];
        _highQuality.layer.cornerRadius = 5.;
        _highQuality.textColor = [UIColor redColor];
        _highQuality.font = [UIFont systemFontOfSize:14.0f];
        _highQuality.backgroundColor = [UIColor clearColor ];
        _highQuality.textAlignment = NSTextAlignmentCenter;
        _highQuality.hidden = YES;
        [self.contentView addSubview:_highQuality];

        titleLabel = [[UILabel alloc]initWithFrame: CGRectMake(CGRectGetMaxX(goodsImage.frame) + MERGIN -5, MERGIN, goodsWidth - CGRectGetMaxX(goodsImage.frame) - MERGIN, TITLE_HEIGHT)];
        titleLabel.font = [UIFont systemFontOfSize: TITLE_FONT_SIZE];
        titleLabel.textColor = [UIColor colorFromHexValue: 0X4d4d4d];
        [self.contentView addSubview: titleLabel];
        
        weightLabel = [[UILabel alloc]initWithFrame: CGRectMake(CGRectGetMaxX(goodsImage.frame) + MERGIN, CGRectGetMaxY(titleLabel.frame), goodsWidth - CGRectGetMaxX(goodsImage.frame) - MERGIN, WEIGHT_HEIGHT)];
        weightLabel.center = CGPointMake(weightLabel.center.x, goodsImage.center.y);
        weightLabel.font = [UIFont systemFontOfSize: WEIGHT_FONT_SIZE];
        weightLabel.textColor = [UIColor colorFromHexValue: 0X9C9C9C];
        [self.contentView addSubview: weightLabel];

//        symbolLabel = [[UILabel alloc]initWithFrame: CGRectMake(CGRectGetMaxX(goodsImage.frame) + MERGIN, GOODS_HEIGHT - MERGIN - TITLE_HEIGHT + 2, SYMBOL_WIDTH, TITLE_HEIGHT)];
//        symbolLabel.font = [UIFont systemFontOfSize: WEIGHT_FONT_SIZE];
//        symbolLabel.textColor = [UIColor redColor];
//        symbolLabel.textAlignment = NSTextAlignmentLeft;
//        [self.contentView addSubview: symbolLabel];
        
        
        priceLabel = [[UILabel alloc]initWithFrame: CGRectMake(CGRectGetMaxX(goodsImage.frame)+ MERGIN, GOODS_HEIGHT - MERGIN - TITLE_HEIGHT, 100, TITLE_HEIGHT)];
        priceLabel.font = [UIFont systemFontOfSize: TITLE_FONT_SIZE];
        priceLabel.textAlignment = NSTextAlignmentLeft;
        priceLabel.textColor = [UIColor redColor];
        [self.contentView addSubview: priceLabel];
        
        addBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [addBtn setFrame: CGRectMake(goodsWidth - ADDBTN_WIDTH - MERGIN, GOODS_HEIGHT - ADDBTN_WIDTH - MERGIN, ADDBTN_WIDTH, ADDBTN_WIDTH)];
        [addBtn setBackgroundImage: [UIImage imageNamed: @"icon-Add"] forState: UIControlStateNormal];
//        [addBtn setBackgroundImage: [UIImage imageNamed: @"icon-Add-On"] forState: UIControlStateHighlighted];
        [addBtn setBackgroundImage: [UIImage imageNamed: @"icon-Add-On"] forState: UIControlStateHighlighted];
        [addBtn setBackgroundImage: [UIImage imageNamed: @"add-1"]forState: UIControlStateDisabled];
        [addBtn addTarget:self action: @selector(addGoodsBtnPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview: addBtn];
        
        buyNumberBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        buyNumberBtn.frame = CGRectMake(CGRectGetMinX(addBtn.frame) - ADDBTN_WIDTH - 3, GOODS_HEIGHT - ADDBTN_WIDTH - MERGIN, ADDBTN_WIDTH, ADDBTN_WIDTH);
        buyNumberBtn.titleLabel.font = [UIFont systemFontOfSize: WEIGHT_FONT_SIZE];
        [buyNumberBtn setTitleColor: [UIColor colorFromHexValue: 0X9C9C9C] forState: UIControlStateNormal];
        [buyNumberBtn addTarget: self action: @selector(setBuyNumberBtnPressed) forControlEvents: UIControlEventTouchUpInside];
        buyNumberBtn.layer.borderWidth = 0.5;
        [self.contentView addSubview: buyNumberBtn];
        //buyNumberBtn.hidden = YES;
 
//        buyNumberLabel.text = @"";
//        buyNumberLabel.textColor = LineColorDefault.CGColor;
        
        subBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [subBtn setFrame: CGRectMake(CGRectGetMinX(buyNumberBtn.frame) - ADDBTN_WIDTH - 3, GOODS_HEIGHT - ADDBTN_WIDTH - MERGIN, ADDBTN_WIDTH, ADDBTN_WIDTH)];
        [subBtn setBackgroundImage: [UIImage imageNamed: @"icon-Minus"] forState: UIControlStateNormal];
//        [subBtn setBackgroundImage: [UIImage imageNamed: @"icon-Minus-On"] forState: UIControlStateHighlighted];
        
        [subBtn setBackgroundImage: [UIImage imageNamed: @"icon-Minus-On"] forState: UIControlStateSelected];
        [subBtn addTarget: self action: @selector(subGoodsBtnPressed) forControlEvents: UIControlEventTouchUpInside];
        subBtn.hidden = YES;
        [self.contentView addSubview: subBtn];
        UIImageView *line=[[UIImageView alloc]initWithFrame: CGRectMake(2, GOODS_HEIGHT - 0.5, goodsWidth - 4, 0.5)];
        [line setBackgroundColor: LineColorDefault];
        [self.contentView addSubview: line];
    }
    return self;
}
/*                                                         原来的参数
 ///品牌ID
 @property (nonatomic, assign) NSUInteger brand_id;
 ///一级分类
 @property (nonatomic, assign) NSUInteger category1_id;
 ///库存
 @property (nonatomic, assign) NSUInteger inventory_qty;
 ///商品ID
 @property (nonatomic, assign) NSUInteger product_id;
 ///商家ID
 @property (nonatomic, assign) NSUInteger shop_id;
 ///商品状态   1为上架
 @property (nonatomic, assign) NSUInteger status;
 ///商品单位
 @property (nonatomic, strong) NSString *model_name;
 ///商品名称
 @property (nonatomic, strong) NSString *product_name;
 ///规格单位
 @property (nonatomic, strong) NSString *weight_name;
 ///规格
 @property (nonatomic, strong) NSString *weight_value;
 ///出货价
 @property (nonatomic, strong) NSDecimalNumber *out_price;
 */

/*
 ///供应商ID
 @property (nonatomic,assign) NSInteger supplierId;
 ///供应商产品ID
 @property (nonatomic,assign) NSInteger productId;
 ///供应商产品库存数
 @property (nonatomic,assign) NSInteger productStock;
 ///供应商产品进货价，精确到小数点后两位
 @property (nonatomic,assign) NSInteger productPrice;
 ///供应商产品税率 无意义
 @property (nonatomic,assign) NSInteger taxRate;
 ///商品图片
 @property (nonatomic,  copy) NSString *photoUrl;
 ///商品名称
 @property (nonatomic,  copy) NSString *merchantProductName;
 ///商品规格
 @property (nonatomic,  copy) NSString *productSpec;
 ///商品ID
 @property (nonatomic,assign) NSInteger merchantProductId;
 ///商品编码
 @property (nonatomic,  copy) NSString *merchantProductCode;
 ///产品编码
 @property (nonatomic,  copy) NSString *productCode;
 ///产品名称
 @property (nonatomic,  copy) NSString *productName;
 ///产品条形码
 @property (nonatomic,  copy) NSString *productBarcode;
 ///产品类目ID
 @property (nonatomic,assign) NSInteger productCategoryId;
 ///产品类目名称
 @property (nonatomic,  copy) NSString *productCategoryName;
 ///产品品牌ID
 @property (nonatomic,assign) NSInteger productBrandId;
 ///产品品牌名称
 @property (nonatomic,  copy) NSString *productBrandName;
 ///是否精品 ,true 精品   false非精品
 @property (nonatomic,assign) NSInteger isHighQuality;
 
 */

-(void)setProductInfo:(JFBOrderGoodsProductDS *)productInfo
{
    _productInfo = productInfo;
    if (productInfo.isHighQuality == 1) {
        _highQuality.hidden = NO;
        titleLabel.frame = CGRectMake(CGRectGetMaxX(_highQuality.frame) + MERGIN-5, titleLabel.frame.origin.y, goodsWidth - CGRectGetMaxX(_highQuality.frame) - MERGIN -5,  titleLabel.frame.size.height);
    } else {
        _highQuality.hidden = YES;
        titleLabel.frame = CGRectMake(CGRectGetMaxX(goodsImage.frame) + MERGIN -5, MERGIN, goodsWidth - CGRectGetMaxX(goodsImage.frame) - MERGIN, TITLE_HEIGHT);
    }
    titleLabel.text = productInfo.merchantProductName;
    if ([productInfo.productSpec isEqualToString: @""]) {
        weightLabel.text = @"";
    } else {
        weightLabel.text = [NSString stringWithFormat:@"%@", productInfo.productSpec];
    }
    priceLabel.text = [NSString stringWithFormat:@"￥%.2f",[productInfo.productPrice floatValue]];
    [goodsImage sd_setImageWithURL: [NSURL URLWithString: [NSString stringWithFormat:@"%@",productInfo.photoUrl]] placeholderImage: [UIImage imageNamed: @"pr_none.jpg"]];
    
    NSInteger count = [[MemoryData sharedMemoryData] getGoodsBuyCount: self.productInfo.productId];
    if (count > 0) {
        [buyNumberBtn setTitle: [NSString stringWithFormat: @"%ld", (long)count] forState: UIControlStateNormal];
        [buyNumberBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //buyNumberBtn.layer.borderColor = [UIColor colorFromHexValue: 0xe1e1e1].CGColor;
        subBtn.hidden = false;
    } else  {
        [buyNumberBtn setTitle: @"" forState: UIControlStateNormal];
        buyNumberBtn.layer.borderColor = [UIColor clearColor].CGColor;
        subBtn.hidden = YES;
    }
    
    if (count >=99)  {
        addBtn.enabled = NO;
    } else  {
        addBtn.enabled = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

-(void)addGoodsBtnPressed
{
    NSInteger carNumber = [[MemoryData sharedMemoryData] getCarGoodsNumber];
    NSInteger buyCount = [[MemoryData sharedMemoryData] getGoodsBuyCount:self.productInfo.productId];
    if (carNumber == 9999 || (carNumber == 9998 && buyCount == 0)) //购物车最多9999件商品
    {
        return;
    }
    if (buyCount >= 98) //每件商品最多买99件
    {
        addBtn.enabled = NO;
    }
    if (buyCount == 0)
    {
        self.productInfo.buyCount = 0;
        [[MemoryData sharedMemoryData].shoppingCarList addObject: self.productInfo];
        [[MemoryData sharedMemoryData] changeGoodsBuyCount: self.productInfo.productId isAdd: YES];
    } else {
        [[MemoryData sharedMemoryData] changeGoodsBuyCount: self.productInfo.productId isAdd: YES];
    }
    
    subBtn.hidden = NO;
    buyNumberBtn.hidden = NO;
    NSString *buyNum = [NSString stringWithFormat:@"%ld", [[MemoryData sharedMemoryData] getGoodsBuyCount:self.productInfo.productId]];
    [buyNumberBtn setTitle: buyNum forState: UIControlStateNormal];
     buyNumberBtn.layer.borderColor = [UIColor colorFromHexValue: 0xe1e1e1].CGColor;
    [self.delegate cellGoodsChange];
}

-(void)subGoodsBtnPressed
{
    [[MemoryData sharedMemoryData] changeGoodsBuyCount: self.productInfo.productId isAdd: NO];
    NSInteger count = [[MemoryData sharedMemoryData] getGoodsBuyCount: self.productInfo.productId];
    if (count == 0) {
        subBtn.hidden = YES;
        [buyNumberBtn setTitle: @"" forState: UIControlStateNormal];
        buyNumberBtn.layer.borderColor = [UIColor clearColor].CGColor;
        buyNumberBtn.hidden = YES;
        [self.delegate cellGoodsChange];
        return;
    }
    if (count < 99) {
        addBtn.enabled = YES;
        buyNumberBtn.hidden = NO;
    }
    NSString *buyNum = [NSString stringWithFormat:@"%ld", count];
    [buyNumberBtn setTitle: buyNum forState: UIControlStateNormal];
     buyNumberBtn.layer.borderColor = [UIColor colorFromHexValue: 0xe1e1e1].CGColor;
    [self.delegate cellGoodsChange];
}

-(void)setBuyNumberBtnPressed
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"修改购买商品数量" message: nil delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];

    [alert setAlertViewStyle: UIAlertViewStylePlainTextInput];
    
    UITextField *textfield=[alert textFieldAtIndex:0];
    [textfield setKeyboardType: UIKeyboardTypeNumberPad];
    
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField *tf = [alertView textFieldAtIndex:0];
    NSString * string = [tf.text trim];
    if ([string isEqualToString:@""]) {
        return;
    }
    
    NSInteger buyNum;
    if (tf.text.length != 0) {
        buyNum =  tf.text.integerValue;
    }
    if (buyNum > 99)
    {
        [JFBUIAnimation alertTips: @"单个商品数量必须小于99"];
        return;
    }
    
    if (buyNum == 0)
    {
        subBtn.hidden = YES;
        [buyNumberBtn setTitle: @"" forState: UIControlStateNormal];
        buyNumberBtn.layer.borderColor = [UIColor clearColor].CGColor;
        buyNumberBtn.hidden = YES;
        [[MemoryData sharedMemoryData] setGoodsBuyCount: self.productInfo.productId withBuyNum: buyNum];
        [[MemoryData sharedMemoryData] changeGoodsBuyCount: self.productInfo.productId isAdd: NO];
        [self.delegate cellGoodsChange];
        return;
    }
    
    if (buyNum == 99)
    {
         addBtn.enabled = NO;
    }
    else
    {
        addBtn.enabled = YES;
        buyNumberBtn.hidden = NO;
    }
    
    NSInteger carNumber = [[MemoryData sharedMemoryData] getCarGoodsNumber];
    NSInteger currentGoodsCount = [[MemoryData sharedMemoryData] getGoodsBuyCount: self.productInfo.productId];
    
    if ((carNumber + buyNum - currentGoodsCount) > 9999)
    {
        [JFBUIAnimation alertTips: @"购物车商品总数不能超过9999"];
        return;
    }
    
    NSString *buyNumStr = [NSString stringWithFormat:@"%ld", (long)tf.text.integerValue];
    
    [[MemoryData sharedMemoryData] setGoodsBuyCount: self.productInfo.productId withBuyNum: buyNum];
    [buyNumberBtn setTitle: buyNumStr forState: UIControlStateNormal];
    [self.delegate cellGoodsChange];
}

@end
